package org.bot.client.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bot.client.driver.api.client.ClientOpenApi;
import org.bot.client.flow.FixedSizeContainer;
import org.bot.client.flow.FlowExecute;
import org.bot.client.util.Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 沧月
 * @create 0004 2023/11/4 22:55
 * @description <description class purpose>
 */
@Component
public class ClientOpenApiImpl implements ClientOpenApi {

    @Autowired
    FlowExecute execute;

    @Override
    public boolean execute(JSONObject flowData, String clientId) {
        return execute.execute(flowData, clientId);
    }

    @Override
    public JSONObject getEventList(String clientId) {
        JSONArray eventList = Bean.getEventList();
        if (clientId == null) {
            clientId = "location";
        }
        return new JSONObject().fluentPut("clientId", clientId)
                .fluentPut("data", eventList);
    }

    @Override
    public boolean interrupt() {
        FixedSizeContainer taskContainer = execute.getTaskContainer();
        taskContainer.forEach(e -> e.setInterrupt(true));
        taskContainer.clear();
        return true;
    }
}
